﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Data;
using Common.Library;
using OnlineVideos.Entities;
using System.ComponentModel;

namespace OnlineMovies.UserControls
{
    public partial class SongsHistory : UserControl
    {
        History objHistory = new History();
        List<LinkHistory> showHistorysongs = null;
        string link = string.Empty;
        int songLastIndex = 1;
        int songFirstIndex = 1;
        int songsHisCount = 0;
        public SongsHistory()
        {
            InitializeComponent();
            showHistorysongs = new List<LinkHistory>();
        }

        private void lbxSongHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxSongHistory.SelectedIndex == -1)
                    return;

                YoutubeApi.Youtube.PlayYoutubeVideo((lbxSongHistory.SelectedItem as ShowLinks).LinkUrl);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxSongHistory_SelectionChanged Method In HistoryPivot file.", ex);
            }
            lbxSongHistory.SelectedIndex = -1;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                songLastIndex = objHistory.GetLastHisId(Constants.SongHistoryFile);
                songFirstIndex = songFirstIndex - 14;
                if (songFirstIndex < 0)
               songFirstIndex = 1;
                songsHisCount = 15;

                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                                   (object s, DoWorkEventArgs a) =>
                                                   {
                                                       a.Result = objHistory.LoadSongHistory(songFirstIndex, songLastIndex, songsHisCount);
                                                   },
                                                   (object s, RunWorkerCompletedEventArgs a) =>
                                                   {


                                                       showHistorysongs = (List<LinkHistory>)a.Result;
                                                       if (showHistorysongs.Count > 0)
                                                       {
                                                           lbxSongHistory.ItemsSource = showHistorysongs;
                                                           tblkHistorySongs.Visibility = Visibility.Collapsed;
                                                       }
                                                       else
                                                       {
                                                           tblkHistorySongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                           tblkHistorySongs.Visibility = Visibility.Visible;
                                                       }
                                                   }
                                                 );
                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadSongHistory Method In HistoryPivot file.", ex);
            }
        }
    }
}
